package cn.com.huzo.webapp.controller.master;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.SupplierGroupEntity;
import cn.com.huzo.service.master.SupplierGroupManager;
import cn.com.huzo.service.master.SupplierGroupMemberSupplierManager;
import cn.com.huzo.service.master.SupplierGroupMemberUserManager;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 供应商组管理控制器
 *
 * @author 吴旭
 */
@Controller
public class SupplierGroupController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "SupplierGroupManager")
    private SupplierGroupManager _manager;

    @Resource(name = "SupplierGroupMemberUserManager")
    private SupplierGroupMemberUserManager _manager_lines_user;

    @Resource(name = "SupplierGroupMemberSupplierManager")
    private SupplierGroupMemberSupplierManager _manager_lines_supplier;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到供应商组一览画面
     * <p>
     * @return
     */
    @RequestMapping("master/supplier_group/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.supplier_group.list.def");
        return view;
    }

    /**
     * 跳转到供应商组编辑画面
     * <p>
     * @param action 操作类别 1：增加 2：编辑
     * @param groupId 供应商组标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("master/supplier_group/{action}/{groupId}/edit.html")
    public ModelAndView redirectEditWnd(@PathVariable(value = "action") int action,
            @PathVariable(value = "groupId") Long groupId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("groupId", groupId);
        view.setViewName("master.supplier_group.edit.def");
        return view;
    }

    /**
     * 弹出供应商组选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/supplier_group/choice.html")
    public ModelAndView redirectSupplierGroupPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.supplier_group.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="供应商组数据处理部分">
    /**
     * 根据标识取得供应商组数据。
     *
     * @param criterion 供应商组标识、成员显示的当前页及每页显示的数据数量
     * @return 供应商组数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/fetchById.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchByHeaderId(criterion);
    }

    /**
     * 根据条件取得系统成员分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 系统成员分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/supplier_group/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 供应商组数据增加或者修改
     *
     * @param instance 要处理的数据
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final SupplierGroupEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getGroupId() > 0) {
            rowsAffected = (long) _manager.update(instance);
        } else {
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="组成员用户数据处理部分">
    /**
     * 根据条件取得供应商成员分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_user/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchUserForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_user.fetchForPage(criterion);
    }

    /**
     * 批量增加供应商组成员。
     *
     * @param criterion 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_user/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchInsertLinesUser(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_user.insert(criterion);
    }

    /**
     * 删除供应商组成员。
     *
     * @param criterion 要删除的成员
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_user/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLinesUser(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_user.delete(criterion);
    }

    /**
     * 删除供应商组成员。
     *
     * @param criterion 要删除的成员
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_user/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDeleteLinesUser(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_user.batchDelete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="组成员供应商数据处理部分">
    /**
     * 根据条件取得供应商分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_supplier/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchSupplierForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_supplier.fetchForPage(criterion);
    }

    /**
     * 批量增加供应商组成员。
     *
     * @param criterion 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_supplier/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchInsertLinesSupplier(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_supplier.insert(criterion);
    }

    /**
     * 删除供应商组成员。
     *
     * @param criterion 要删除的成员
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_supplier/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLinesSupplier(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_supplier.delete(criterion);
    }

    /**
     * 删除供应商组所属供应商。
     *
     * @param criterion 要删除的数据
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/supplier_group/lines_supplier/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDeleteLinesSupplier(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines_supplier.batchDelete(criterion);
    }
    // </editor-fold>
}
